package ar.com.mooral.activities;


import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import ar.com.mooral.crohn.Crohn;
import ar.com.mooral.crohn.CrohnAlarmReceiver;
import ar.com.mooral.crohn.Synchronizer;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.data.Alarms;
import ar.com.mooral.data.Consultations;
import ar.com.mooral.data.DatabaseHelper;
import ar.com.mooral.data.Depositions;
import ar.com.mooral.data.Doctors;
import ar.com.mooral.data.FoodList;
import ar.com.mooral.data.Foods;
import ar.com.mooral.data.GeneralStatus;
import ar.com.mooral.data.Medications;
import ar.com.mooral.data.MedicationsList;
import ar.com.mooral.data.MissedMedicationReason;
import ar.com.mooral.data.MissedMedications;
import ar.com.mooral.data.Pain;
import ar.com.mooral.data.PatientSymptoms;
import ar.com.mooral.data.Patients;
import ar.com.mooral.data.SexualActivityIncidence;
import ar.com.mooral.data.SocialActivityIncidence;
import ar.com.mooral.data.Symptoms;
import ar.com.mooral.data.WorkActivityIncidence;

public class Splash extends Activity {
    
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        
        // If no username is set, ask for one, otherwise go straight to the menu 
        boolean dbInitialize = this.getSharedPreferences(Crohn.PREFS_NAME, 0).getBoolean("dbInitialize", false);
        String username = this.getSharedPreferences(Crohn.PREFS_NAME, 0).getString("username", "");
        String password = this.getSharedPreferences(Crohn.PREFS_NAME, 0).getString("password", "");
        String patientCode = this.getSharedPreferences(Crohn.PREFS_NAME, 0).getString("patientCode", "");
        

        // Check if we need to initialize database
        if (!dbInitialize) {
        	
        	/*
        	Context context = getApplicationContext();
        	CharSequence text = "Esta operacion puede tomar unos segundos. Aguarde por favor...";
        	int duration = Toast.LENGTH_SHORT;

        	Toast toast = Toast.makeText(context, text, duration);
        	toast.show();
        	*/

        	
            Synchronizer sync = new Synchronizer(this);
        	DatabaseHelper dbs[] = new DatabaseHelper[] {
    				new Consultations(this),
    				new Depositions(this),
    				new Doctors(this),
    				new FoodList(this),
    				new Foods(this),
    				new GeneralStatus(this),
    				new Medications(this),
    				new MedicationsList(this),
    				new MissedMedicationReason(this),
    				new MissedMedications(this),
    				new Pain(this),
    				new Patients(this),
    				new PatientSymptoms(this),
    				new SexualActivityIncidence(this),
    				new SocialActivityIncidence(this),
    				new Symptoms(this),
    				new WorkActivityIncidence(this),
    				new Alarms(this)};

        	for (int i = 0; i< dbs.length; i++) {
        		dbs[i].create();
        	}
        	
    		SharedPreferences.Editor editor = this.getSharedPreferences(Crohn.PREFS_NAME, 0).edit();
    		editor.putBoolean("dbInitialize", true);
    		editor.commit();
        }
        

        
        // if already logged in, go right to the menu
        if (username.length() > 0 && password.length() > 0 && patientCode.length() > 0) {
        	
        	Synchronizer sync = new Synchronizer(this);
        	sync.receiveUpdates();
        	
    		Intent intent = new Intent();
	        CrohnContext.setPassword(password);
	        CrohnContext.setUsername(username);
	        CrohnContext.setPatientCode(patientCode);
	        //intent.setComponent(new ComponentName(MenuPrincipal.class.getPackage().getName(), MenuPrincipal.class.getName()));
	        //intent.setComponent(new ComponentName(Terminos.class.getPackage().getName(), Terminos.class.getName()));
	        intent.setComponent(new ComponentName(Main.class.getPackage().getName(), Main.class.getName()));
	        startActivity(intent);
        }
        

        
        // USUARIO REGISTRADO
		Button btnSplashUsuarioRegistrado = (Button) findViewById(R.id.btnSplashUsuarioRegistrado);
		btnSplashUsuarioRegistrado.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(Login.class.getPackage().getName(), Login.class.getName()));
				startActivity(intent);
		    }
		});		


        // REGISTRAR
		Button btnSplashRegistrese = (Button) findViewById(R.id.btnSplashRegistrese);
		btnSplashRegistrese.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				//intent.setComponent(new ComponentName(CodigoMedico.class.getPackage().getName(), CodigoMedico.class.getName()));
				intent.setComponent(new ComponentName(Terminos.class.getPackage().getName(), Terminos.class.getName()));
				startActivity(intent);
		    }
		});		

        
    }
    
}